<?php
require 'db.php'; // 包含数据库连接
require 'vendor/autoload.php'; // 引入 Composer 自动加载器
include_once 'secret/jwt.php';

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

if (!isset($_COOKIE['token'])) {
    header('Location: index.php');
    exit();
}

$jwt = $_COOKIE['token'];

try {
    $decoded = JWT::decode($jwt, new Key($secretKey, 'HS256'));
    $username = $decoded->data->username;
    $role = $decoded->data->role;
} catch (Exception $e) {
    echo json_encode([
        'error' => '访问被拒绝: ' . $e->getMessage()
    ]);
    exit();
}

if($role!=="admin"){
    header('Location: profile.html');
    exit();
}

// 获取所有待审核的文章
$sql = "SELECT id, title, author_name, created_at FROM articles WHERE status = 'pending' ORDER BY created_at DESC";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html>
<head>
    <title>管理员审核文章</title>
</head>
<body>
    <h2>待审核文章列表</h2>
    <ul>
        <?php
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                echo "<li><a href='admin_review_detail.php?id=" . $row['id'] . "'>" . htmlspecialchars($row['title']) . "</a> by " . htmlspecialchars($row['author_name']) . " on " . $row['created_at'] . "</li>";
            }
        } else {
            echo "<li>暂无待审核文章</li>";
        }
        ?>
    </ul>
    <!-- 返回按钮 -->
    <a href="profile.html">返回个人资料页面</a>
</body>
</html>

<?php
$conn->close();
?>
